package com.example.theorygame.layer;

import android.view.MotionEvent;

import com.example.theorygame.base.BaseElement;
import com.example.theorygame.entity.blue;
import com.example.theorygame.entity.brown;
import com.example.theorygame.entity.pink;
import com.example.theorygame.entity.purple;
import com.example.theorygame.entity.yellow;
import com.example.theorygame.util.CommouUtils;
import com.example.theorygame.util.CompareEach;

import org.cocos2d.actions.CCScheduler;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;

import java.util.Arrays;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 多次大赛
 *
 * @author yangfan
 */
public class FifthLayer extends BaseLayer {
    /**
     * 第一区域：变量定义区域
     */
    //1.背景
    private CCSprite bg;
    private CCSprite background;
    //2.go按钮 go按钮锁
    private CCSprite go;
    private boolean gorook = true;
    //3.初始化人物列表
    private CopyOnWriteArrayList<blue> mblue = new CopyOnWriteArrayList<blue>();
    private CopyOnWriteArrayList<purple> mpurple = new CopyOnWriteArrayList<purple>();
    private CopyOnWriteArrayList<pink> mpink = new CopyOnWriteArrayList<pink>();
    private CopyOnWriteArrayList<BaseElement> baseMan = new CopyOnWriteArrayList<BaseElement>();
    //4.初始化人物字符串  1 2 3 4 5
    private int getman[] = new int[25];
    //5.初始化坐标  X  Y
    private int myX[] = new int[25];
    private int myY[] = new int[25];
    //6.定义文字界面  人物选择按钮  人物锁
    private CCLabel t1, t2, t3, t4, t5, t6, t7;
    private CCSprite pinkB, blueB, purpleB;
    private boolean manrook = false;
    //7.定义比赛按钮
    private CCSprite startButton, remove5Button, new5Button;
    //8.开始比赛区域   得分数组  双方选择 比赛锁
    private int score[] = new int[25];
    private int chose1, chose2;
    private CompareEach c = new CompareEach();
    private int up1, up2;
    private boolean startcomparerook = false;
    //9.i
    private int i;
    //10.按钮锁 开始 建屋 家务
    private int click = 1;
    //11.最低五值 最高五只数组  得分
    private int min;
    private int max;
    //12.定义透明
    private CCSprite tou;
    //13.局数
    private int t;
    //14.显示文字
    private int showText = 0;
    //15.定义对了按钮
    private CCSprite gotrue;
    private boolean gotruerook = false;
    //16.定义开始进化按钮
    private CCSprite startfivego;
    private boolean startfivegorook = false;
    //17.定义第五页面文字
    private CCLabel t51, t52, t53, t54, t55, t56, t57;
    private CCSprite proButton;
    private boolean proButtonrook = false;
    //18.切换页面
    private CCSprite newproButton;
    private boolean newprorook = false;
    //19.血
    private CCSprite bleed;

    /**
     * 第二区域：构造方法区域
     */
    public FifthLayer() {
        //位置
        myX = new int[]{103, 135, 164, 188, 210, 226, 235, 236, 232, 219, 199, 175, 149, 120, 90, 62, 39, 20, 6, 2, 5, 15, 30, 50, 76};
        myY = new int[]{25, 25, 33, 45, 66, 91, 119, 145, 172, 202, 224, 240, 252, 256, 253, 241, 224, 200, 179, 148, 118, 91, 64, 46, 30};
        //定义背景
        bg = CCSprite.sprite("image/layer5/bg5.png");
        bg.setAnchorPoint(0, 0);
        bg.setPosition(0, 0);
        this.addChild(bg, 0);
        //定义试试按钮
        go = CCSprite.sprite("image/layer5/go.png");
        go.setAnchorPoint(0, 0);
        go.setPosition(277, 0);
        go.setScale(0.6f);
        this.addChild(go, 1);

        //设置触摸
        setIsTouchEnabled(true);
    }

    /**
     * 第三区域：触摸方法区域
     */
    @Override
    public boolean ccTouchesBegan(MotionEvent event) {
        CGPoint conver = this.convertTouchToNodeSpace(event);
        if (newprorook) {
            if (CGRect.containsPoint(newproButton.getBoundingBox(), conver)) {
                CommouUtils.changeLayer(new SixthLayer());
            }
        }
        if (proButtonrook) {
            if (CGRect.containsPoint(proButton.getBoundingBox(), conver)) {
                //清除原来精灵
                tou.removeSelf();
                t2.removeSelf();
                startfivego.removeSelf();
                t51.removeSelf();
                t52.removeSelf();
                t53.removeSelf();
                t54.removeSelf();
                t55.removeSelf();
                t56.removeSelf();
                t57.removeSelf();
                proButton.removeSelf();
                //添加精灵


                allbad();

                newprorook = true;
            }
        }
        if (startfivegorook) {
            if (CGRect.containsPoint(startfivego.getBoundingBox(), conver)) {
                //运行线程
                //1.设置线程
                CCScheduler scheduler = CCScheduler.sharedScheduler();
                scheduler.schedule("startgot", this, 0.2f, false);
                //设置页面

                t51 = CCLabel.labelWithString("（注意：有少数黑帮老铁可能会一直留下来，因为", "圆体", 10);
                t52 = CCLabel.labelWithString("除了黑帮老铁和复读机之外所有人都被消灭了，他", "圆体", 10);
                t53 = CCLabel.labelWithString("们两个会达成平局。）", "圆体", 10);
                t54 = CCLabel.labelWithString("所以，看起来博弈论的道理好像在告诉我们这些事", "圆体", 10);
                t55 = CCLabel.labelWithString("情：像复读机这样「己所不欲勿施于人」的人生哲", "圆体", 10);
                t56 = CCLabel.labelWithString("学不仅仅是道德层面上的真理，同时也是科学上的", "圆体", 10);
                t57 = CCLabel.labelWithString("真理。然而...", "圆体", 10);
                t51.setAnchorPoint(0, 0);
                t52.setAnchorPoint(0, 0);
                t53.setAnchorPoint(0, 0);
                t54.setAnchorPoint(0, 0);
                t55.setAnchorPoint(0, 0);
                t56.setAnchorPoint(0, 0);
                t57.setAnchorPoint(0, 0);
                t51.setColor(ccc3(0, 0, 0));
                t52.setColor(ccc3(0, 0, 0));
                t53.setColor(ccc3(0, 0, 0));
                t54.setColor(ccc3(0, 0, 0));
                t55.setColor(ccc3(0, 0, 0));
                t56.setColor(ccc3(0, 0, 0));
                t57.setColor(ccc3(0, 0, 0));
                t51.setPosition(260, 185);
                t52.setPosition(260, 160);
                t53.setPosition(260, 145);
                t54.setPosition(260, 120);
                t55.setPosition(260, 105);
                t56.setPosition(260, 90);
                t57.setPosition(260, 75);
                this.addChild(t51);
                this.addChild(t52);
                this.addChild(t53);
                this.addChild(t54);
                this.addChild(t55);
                this.addChild(t56);
                this.addChild(t57);
                proButton = CCSprite.sprite("image/layer5/pro.png");
                proButton.setAnchorPoint(0, 0);
                proButton.setPosition(260, 10);
                proButton.setScale(0.6f);
                this.addChild(proButton);
                proButtonrook = true;
            }
        }
        if (gotruerook) {
            if (CGRect.containsPoint(gotrue.getBoundingBox(), conver)) {
                fivemanPanel();
            }
        }
        if (startcomparerook) {
            //繁衍5个最高分
            if (click % 3 == 0) {
                if (CGRect.containsPoint(new5Button.getBoundingBox(), conver)) {
                    add();
                    bleed.removeSelf();
                    if (showText == 1) {
                        t1.setString("好吧！这次老油条获得了胜利，数量增加");
                        t2.setString("了五个。 不过，我们再多玩几场试试...");
                    }
                    if (showText == 2) {
                        t1.setString("随着小粉红被五个五个地吃掉，老油条越来越多...");
                        t2.setString("...");
                    }
                    if (showText == 3) {
                        t1.setString("现在，小粉红们全部被吃掉了。不过，等等...");
                        t2.setString("...");
                    }
                    if (showText == 4) {
                        t1.setString("对啦：老油条作茧自缚，变成了受害者！他们剥削");
                        t2.setString("完了天真无邪的小粉红");
                    }
                    if (showText == 5) {
                        t1.setString("所以，结果显然就是...");
                        t2.setString("...");
                    }
                    if (showText == 6) {
                        t1.setString("所以，结果显然就是...");
                        t2.setString("...复读机继承了地球。");
                        remove5Button.removeSelf();
                        new5Button.removeSelf();
                        startButton.removeSelf();
                        //关闭按钮锁
                        startcomparerook = false;
                        //定义对了按钮
                        gotrue = CCSprite.sprite("image/layer5/gotrue.png");
                        gotrue.setAnchorPoint(0, 0);
                        gotrue.setPosition(280, 30);
                        this.addChild(gotrue);
                        gotruerook = true;
                    }

                    //设置锁

                    remove5Button.setOpacity(100);
                    new5Button.setOpacity(100);
                    startButton.setOpacity(255);
                }
            }
            //去除5个最低分
            if (click % 3 == 2) {
                if (CGRect.containsPoint(remove5Button.getBoundingBox(), conver)) {
                    bleed = CCSprite.sprite("image/layer5/bleed.png");
                    bleed.setAnchorPoint(0, 0);
                    bleed.setScale(2f);
                    bleed.setPosition(0, 0);
                    this.addChild(bleed);
                    sub();
                    //设置锁
                    startButton.setOpacity(100);
                    remove5Button.setOpacity(100);
                    new5Button.setOpacity(255);
                    showText += 1;

                }
            }

            //开始
            if (click % 3 == 1) {
                if (CGRect.containsPoint(startButton.getBoundingBox(), conver)) {
                    sta();
                    sub();
                    add();
                    sta();
                    sub();
                    add();

                    sta();
                    //设置锁
                    startButton.setOpacity(100);
                    remove5Button.setOpacity(255);
                    new5Button.setOpacity(100);

                }
            }


        }

        if (manrook) {
            if (CGRect.containsPoint(blueB.getBoundingBox(), conver)) {
                t1.setString("有道理，复读机刚刚就赢了嘛，这次怎么会输");
                t2.setString("呢？... 好，现在看看你选的对不对：");
                comparePanel();
                //关闭人物锁
                manrook = false;
                startcomparerook = true;
            }
            if (CGRect.containsPoint(pinkB.getBoundingBox(), conver)) {
                t1.setString("有道理，万年小粉红人多力量大嘛... 好，现在看看");
                t2.setString("你选的对不对：");
                comparePanel();
                //关闭人物锁
                manrook = false;
                startcomparerook = true;
            }
            if (CGRect.containsPoint(purpleB.getBoundingBox(), conver)) {
                t1.setString("有道理，千年老油条 有这么多万年小粉红可以取剥");
                t2.setString("削... 好，现在看看你选的对不对：");
                comparePanel();
                //关闭人物锁
                manrook = false;
                startcomparerook = true;
            }
        }


        if (gorook) {
            if (CGRect.containsPoint(go.getBoundingBox(), conver)) {
                background = CCSprite.sprite("image/bg.png");
                background.setAnchorPoint(0, 0);
                background.setPosition(0, 0);
                this.addChild(background);
                bg.removeFromParentAndCleanup(true);
                go.removeFromParentAndCleanup(true);

                getman = new int[]{3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
                tou = CCSprite.sprite("image/layer5/tou.png");
                tou.setAnchorPoint(0, 0);
                tou.setPosition(0, 0);
                this.addChild(tou);
                manCompare(getman, false);

                //显示文字
                t1 = CCLabel.labelWithString("我们开始先用这些玩家来进行比赛： 十五个万年", "圆体", 10);
                t2 = CCLabel.labelWithString("小粉红， 五个千年老油条，还有 五个复读机. （", "圆体", 10);
                t3 = CCLabel.labelWithString("我们现在暂时先把黑帮老铁和福尔摩星儿放一边）", "圆体", 10);
                t4 = CCLabel.labelWithString("我们马上开始「比赛-淘汰-繁殖」三部曲，然后", "圆体", 10);
                t5 = CCLabel.labelWithString("重复差不多十几场吧。好，现在我们再来猜一个！", "圆体", 10);
                t6 = CCLabel.labelWithString("你觉得第一场下来，谁会赢呢？ 请再次做出你的", "圆体", 10);
                t7 = CCLabel.labelWithString("选择:", "圆体", 10);
                t1.setAnchorPoint(0, 0);
                t2.setAnchorPoint(0, 0);
                t3.setAnchorPoint(0, 0);
                t4.setAnchorPoint(0, 0);
                t5.setAnchorPoint(0, 0);
                t6.setAnchorPoint(0, 0);
                t7.setAnchorPoint(0, 0);
                t1.setColor(ccc3(0, 0, 0));
                t2.setColor(ccc3(0, 0, 0));
                t3.setColor(ccc3(0, 0, 0));
                t4.setColor(ccc3(0, 0, 0));
                t5.setColor(ccc3(0, 0, 0));
                t6.setColor(ccc3(0, 0, 0));
                t7.setColor(ccc3(0, 0, 0));
                t1.setPosition(260, 270);
                t2.setPosition(260, 255);
                t3.setPosition(260, 240);
                t4.setPosition(260, 220);
                t5.setPosition(260, 205);
                t6.setPosition(260, 190);
                t7.setPosition(260, 175);
                this.addChild(t1);
                this.addChild(t2);
                this.addChild(t3);
                this.addChild(t4);
                this.addChild(t5);
                this.addChild(t6);
                this.addChild(t7);
                //添加按钮
                blueB = CCSprite.sprite("image/layer4/sprite1.png");
                purpleB = CCSprite.sprite("image/layer4/sprite3.png");
                pinkB = CCSprite.sprite("image/layer4/sprite2.png");
                blueB.setAnchorPoint(0, 0);
                purpleB.setAnchorPoint(0, 0);
                pinkB.setAnchorPoint(0, 0);
                blueB.setPosition(260, 90);
                pinkB.setPosition(290, 50);
                purpleB.setPosition(320, 10);
                blueB.setScale(0.6);
                pinkB.setScale(0.6);
                purpleB.setScale(0.6);
                this.addChild(blueB);
                this.addChild(pinkB);
                this.addChild(purpleB);
                //关闭选择锁 开启人物锁
                gorook = false;
                manrook = true;
            }
        }
        return true;

    }

    /**
     * 第四区域：方法定义区域
     */
    //显示人物
    public void manCompare(int[] man, boolean b) {
        for (i = 0; i < 25; i++) {
            if (man[i] == 0) {
                for (final BaseElement element : baseMan) {
                    element.getParent().removeSelf();
                }

            }
            if (man[i] == 1) {
                blue blue1 = new blue(myX[i], myY[i], b, score[i], i);
                tou.addChild(blue1);
                baseMan.add(blue1);

            }
            if (man[i] == 2) {
                purple purple1 = new purple(myX[i], myY[i], b, score[i], i);
                tou.addChild(purple1);
                baseMan.add(purple1);
            }
            if (man[i] == 3) {
                pink pink1 = new pink(myX[i], myY[i], b, score[i], i);
                tou.addChild(pink1);
                baseMan.add(pink1);
            }
            if (man[i] == 4) {
                yellow yellow1 = new yellow(myX[i], myY[i], b, score[i], i);
                tou.addChild(yellow1);
                baseMan.add(yellow1);
            }
            if (man[i] == 5) {
                brown brown1 = new brown(myX[i], myY[i], b, score[i], i);
                tou.addChild(brown1);
                baseMan.add(brown1);
            }

        }
    }

    public void comparePanel() {
        t3.removeSelf();
        t4.removeSelf();
        t5.removeSelf();
        t6.removeSelf();
        t7.removeSelf();
        blueB.removeSelf();
        pinkB.removeSelf();
        purpleB.removeSelf();
        startButton = CCSprite.sprite("image/layer5/startButton.png");
        startButton.setAnchorPoint(0, 0);
        startButton.setScale(0.9);
        startButton.setPosition(260, 100);
        remove5Button = CCSprite.sprite("image/layer5/removeButton.png");
        remove5Button.setAnchorPoint(0, 0);
        remove5Button.setScale(0.9);
        remove5Button.setPosition(260, 50);
        new5Button = CCSprite.sprite("image/layer5/newButton.png");
        new5Button.setAnchorPoint(0, 0);
        new5Button.setScale(0.9);
        new5Button.setPosition(260, 0);
        remove5Button.setOpacity(100);
        new5Button.setOpacity(100);
        this.addChild(startButton);
        this.addChild(remove5Button);
        this.addChild(new5Button);
    }

    //互相比较 开始比赛
    public void CompareEachOther(int[] man) {


        for (i = 0; i < man.length; i++) {   //25局比赛
            for (int j = i + 1; j < man.length; j++) {  //每个人和他后面的人比赛
                for (t = 0; t < 10; t++) {  //每局进行10次比赛   t  局数
                    //判断每个人的选择
                    chose1 = getChose(man[i], up1);
                    //选择二
                    chose2 = getChose(man[j], up2);
                    //修改上次对方选择
                    if (chose1 == 0 && chose2 == 0) {
                        //互相欺骗 都加0  man[i] man[j]+0
                        score[i] = score[i] + 0;
                        score[j] = score[j] + 0;
                    }
                    if (chose1 == 1 && chose2 == 1) {
                        //互相合作 都加2 man[i] man[j]+2
                        score[i] = score[i] + 2;
                        score[j] = score[j] + 2;
                    }
                    if (chose1 == 0 && chose2 == 1) {
                        //man[i]+3 man[j]-1
                        score[i] = score[i] + 3;
                        score[j] = score[j] - 1;
                    }
                    if (chose1 == 1 && chose2 == 0) {
                        //man[i]-1 man[j]+3
                        score[i] = score[i] - 1;
                        score[j] = score[j] + 3;
                    }
                    if (t == 0) {
                        up1 = up2 = 3;
                    } else {
                        up1 = chose2;
                        up2 = chose1;
                    }
                }
            }
        }
    }

    //获得选择方法
    public int getChose(int mchose, int up) {
        switch (mchose) {
            case 1:
                mchose = c.blueP(up, t);
                break;
            case 2:
                mchose = c.purpleP();
                break;
            case 3:
                mchose = c.pinkP();
                break;
            case 4:
                mchose = c.yellowP(up, t);
                break;
            case 5:
                mchose = c.brownP(up, t);
                break;
            case 6:
                break;
            case 7:
                break;
            case 8:
                break;
        }
        return mchose;
    }

    //获取数组最大值
    public static int[] getMaxIndex(int[] arr) {
        //如果数组为空 或者是长度为0 就返回null
        if (arr == null || arr.length == 0) {
            return null;
        }
        //假设第一个元素为最大值 那么下标设为0
        int maxIndex = 0;
        //设置一个长度为2的数组用作记录，第一个元素存储最大值，第二个元素存储下标
        int[] arrnew = new int[2];
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[maxIndex] < arr[i + 1]) {
                maxIndex = i + 1;
            }
        }
        arrnew[0] = arr[maxIndex];
        arrnew[1] = maxIndex;
        return arrnew;
    }

    //获取数组最小值
    public static int[] getMinIndex(int[] arr) {
        if (arr == null || arr.length == 0) {
            return null;
        }
        int minIndex = 0;
        int[] arrnew = new int[2];
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[minIndex] > arr[i + 1]) {
                minIndex = i + 1;
            }
        }
        arrnew[0] = arr[minIndex];
        arrnew[1] = minIndex;
        return arrnew;
    }

    public void sta() {
        System.out.println("开始");
        CompareEachOther(getman);
        manCompare(getman, true);
        click += 1;

    }

    public void sub() {
        System.out.println("去除最低分");
        //取五个最低分索引    最高分索引
        min = getMinIndex(score)[1];       //添加最小数到minscore
        max = getMaxIndex(score)[1];       //最大指引
        //修改getman  移除最低分  移除精灵
        Arrays.fill(getman, min, min + 1, 0);

        manCompare(getman, true);
        Arrays.fill(score, 0);
        //得分归零

        click += 1;
    }

    public void add() {
        System.out.println("添加最高分");
        Arrays.fill(getman, min, min + 1, getman[max]);

        tou = CCSprite.sprite("image/layer5/tou.png");
        tou.setAnchorPoint(0, 0);
        tou.setPosition(0, 0);
        this.addChild(tou);
        manCompare(getman, false);
        click += 1;
    }

    //5人比赛界面
    public void fivemanPanel() {
        tou.removeSelf();
        gotrue.removeSelf();
        getman = new int[]{1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5};
        tou = CCSprite.sprite("image/layer5/tou.png");
        tou.setAnchorPoint(0, 0);
        tou.setPosition(0, 0);
        this.addChild(tou);
        manCompare(getman, false);
        t1.setString("(...如果把 黑帮老铁和福尔摩星儿都放进来结果其");
        t2.setString("实也差不多:)");
        startfivego = CCSprite.sprite("image/layer5/startgofive.png");
        startfivego.setAnchorPoint(0, 0);
        startfivego.setPosition(260, 180);
        startfivego.setScale(0.6f);
        this.addChild(startfivego);
        startfivegorook = true;
    }

    //线程运行方法
    public void startgot() {
        if (true) {
            CompareEachOther(getman);
            manCompare(getman, true);

            min = getMinIndex(score)[1];       //添加最小数到minscore
            max = getMaxIndex(score)[1];       //最大指引
            //修改getman  移除最低分  移除精灵
            Arrays.fill(getman, min, min + 1, 0);
            manCompare(getman, true);
            Arrays.fill(score, 0);

            Arrays.fill(getman, min, min + 1, getman[max]);

            tou = CCSprite.sprite("image/layer5/tou.png");
            tou.setAnchorPoint(0, 0);
            tou.setPosition(0, 0);
            this.addChild(tou);
            manCompare(getman, false);
        }
    }

    public void allbad() {
        t1.setString("你看，这个世界充满了无赖！");

        tou = CCSprite.sprite("image/layer5/tou.png");
        tou.setAnchorPoint(0, 0);
        tou.setPosition(0, 0);
        this.addChild(tou);
        getman = new int[]{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
        manCompare(getman, false);

        newproButton = CCSprite.sprite("image/layer5/what.png");
        newproButton.setAnchorPoint(0, 0);
        newproButton.setPosition(260, 80);
        this.addChild(newproButton);
    }
}




















